package com.haoniu.web.controller.module;

import com.haoniu.common.annotation.Log;
import com.haoniu.common.base.AjaxResult;
import com.haoniu.common.enums.BusinessType;
import com.haoniu.common.support.Convert;
import com.haoniu.framework.util.ShiroUtils;
import com.haoniu.framework.web.page.TableDataInfo;
import com.haoniu.module.domain.database.GroupPlay;
import com.haoniu.module.service.GroupPlayService;
import com.haoniu.web.core.base.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * qiuboyang
 */
@Controller
@RequestMapping("/module/groupPlay")
public class GroupPlayController extends BaseController {
    private String prefix = "module/groupPlay";

    @Autowired
    private GroupPlayService groupPlayService;


    @RequiresPermissions("module:groupPlay:view")
    @GetMapping()
    public String groupPlay() {
        return prefix + "/groupPlay";
    }

    @RequiresPermissions("module:groupPlay:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GroupPlay queryGroupPlay) {
        startPage();
        List<GroupPlay> list = groupPlayService.selectGroupPlayList(queryGroupPlay);
        for (GroupPlay groupPlay:list) {
            String imgUrl = groupPlay.getImgUrl();
            if(StringUtils.isNotBlank(imgUrl)){
                String[] imgArray = Convert.toStrArray(imgUrl);
                groupPlay.setImgUrl(imgArray[0]);
            }
        }
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String add() {

        return prefix + "/add";
    }

    /**
     * 新增保存规则图
     */
    @RequiresPermissions("module:groupPlay:add")
    @Log(title = "规则图", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GroupPlay groupPlay) {
        groupPlay.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(groupPlayService.insertGroupPlay(groupPlay));
    }

    /**
     * 修改规则图
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        GroupPlay groupPlay = groupPlayService.selectGroupPlayById(id);
        mmap.put("groupPlay", groupPlay);
        return prefix + "/edit";
    }

    /**
     * 修改保存规则图
     */
    @RequiresPermissions("module:groupPlay:edit")
    @Log(title = "规则图", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GroupPlay groupPlay) {
        groupPlay.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(groupPlayService.updateGroupPlay(groupPlay));
    }

    /**
     * 删除规则图
     */
    @RequiresPermissions("module:groupPlay:remove")
    @Log(title = "规则图", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(groupPlayService.deleteGroupPlayByIds(ids));
    }

}
